--[[模拟器分辨率：720*400 DPI：106
脚本入口函数： Main(dm_OBJ,hwnd)
dm_OBJ：大漠对象
hwnd：模拟器主窗口句柄
Main函数末尾加return xxx
xxx：可自定义任何反馈给主程序的值，不能为nil
*脚本过程中使用的大漠函数名、参数等参照大漠说明文档
]]
g_rcpath = ""
function currDir()
	os.execute("cd > cd.tmp")
	local f = io.open("cd.tmp", r)
	local cwd = f:read("*a")
	f:close()
	os.remove("cd.tmp")
	cwd= string.sub(cwd, 1, -2)
	print(cwd)
	return cwd
end


function md5_sumhexa(k)
	str = currDir()
	str = str .. '\\md5core.dll'
	local md5_core = package.loadlib(str,"luaopen_md5_core")
	
	--local md5_core = require "md5core"
	--k = md5_core.sum(k)
	l= md5_core()
	----[[
	k = l.sum(k)
	return (string.gsub(k, ".",
	function (c)
		return string.format("%02x", string.byte(c))
	end
	)
	)
	--]]
	--crypt
	--[[
	for k1, v in pairs(l) do
		
		print(k1,v)
	end
	
	--]]
	
end

function ToBase64(source_str)
	local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
	local s64 = ''
	local str = source_str
	
	while #str > 0 do
		local bytes_num = 0
		local buf = 0
		
		for byte_cnt=1,3 do
			buf = (buf * 256)
			if #str > 0 then
				buf = buf + string.byte(str, 1, 1)
				str = string.sub(str, 2)
				bytes_num = bytes_num + 1
			end
		end
		
		for group_cnt=1,(bytes_num+1) do
			b64char = math.fmod(math.floor(buf/262144), 64) + 1
			s64 = s64 .. string.sub(b64chars, b64char, b64char)
			buf = buf * 64
		end
		
		for fill_cnt=1,(3-bytes_num) do
			s64 = s64 .. '='
		end
	end
	
	return s64
end

function FromBase64(str64)
	local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
	local temp={}
	for i=1,64 do
		temp[string.sub(b64chars,i,i)] = i
	end
	temp['=']=0
	local str=""
	for i=1,#str64,4 do
		if i>#str64 then
			break
		end
		local data = 0
		local str_count=0
		for j=0,3 do
			local str1=string.sub(str64,i+j,i+j)
			if not temp[str1] then
				return
			end
			if temp[str1] < 1 then
				data = data * 64
			else
				data = data * 64 + temp[str1]-1
				str_count = str_count + 1
			end
		end
		for j=16,0,-8 do
			if str_count > 0 then
				str=str..string.char(math.floor(data/math.pow(2,j)))
				data=math.mod(data,math.pow(2,j))
				str_count = str_count - 1
			end
		end
	end
	return str
end





function Split(str, delim, maxNb)  --分割字符串
	-- Eliminate bad cases...
	if string.find(str, delim) == nil then
		return { str }
	end
	if maxNb == nil or maxNb < 1 then
		maxNb = 0    -- No limit
	end
	local result = {}
	local pat = "(.-)" .. delim .. "()"
	local nb = 0
	local lastPos
	for part, pos in string.gfind(str, pat) do
		nb = nb + 1
		result[nb] = part
		lastPos = pos
		if nb == maxNb then break end
	end
	if nb ~= maxNb then
		result[nb + 1] = string.sub(str, lastPos)
	end
	return result
end



function readAccount(dm_OBJ,rcpath,hwnd) -- 读取本地账号密码
	local accountTable = {}
	local sTable = {}
	local title = dm_OBJ:GetWindowTitle(tonumber(hwnd))
	
	local myFlie = io.open(rcpath..title..".txt","r")
	if myFlie == nil then
		print("打开文件出错")
	else
		print("打开文件成功，可以进行读取操作\n")
		while true do
			line = myFlie:read("*l")
			if line == nil  then
				break
			end
			
			print(line)
			
			sTable = Split(line,"-")
			
			table.insert(accountTable,sTable)
			
		end
	end
	if myFlie ~= nil then
		myFlie:close()
	end
	
	
	
	return accountTable
end
function readtxt(dm_OBJ,rcpath,hwnd,ltype) -- 读取本地账号密码
	local line = ""
	local title = dm_OBJ:GetWindowTitle(tonumber(hwnd))
	
	line = dm_OBJ:ReadIni("kingRobot.lua",ltype,rcpath..title..".ini")
	
	return line
end



function ParseCoord(str)  --返回由索引1开始的值，retStr[1][2] == -1 则找不到图
	local retStr = {}
	local temprStr = {}
	temprStr = Split(str, '|')
	for i = 1,#temprStr do
		retStr[i] = Split(temprStr[i], ",")
	end
	return retStr
end

function FindPicCheck(dm_OBJ,x1, y1, x2, y2, pic_name, delta_color,sim,f_type)
	local pic_ret
	local coord = {}
	pic_ret = dm_OBJ:FindPicEx(x1, y1, x2, y2, pic_name, delta_color,sim, 0)
	coord = ParseCoord(pic_ret)
	if tonumber(coord[1][2]) > 0 then
		if f_type == "click" then
			dm_OBJ:MoveTo(tonumber(coord[1][2]),tonumber(coord[1][3]))
			dm_OBJ:LuaSleep(1000)
			dm_OBJ:LeftClick()
			return true
		elseif f_type == "ret" then
			return ret
		end
	else
		pic_ret = nil
	end
	return pic_ret
end

function Outcry(dm_OBJ,hwnd,connet,num,userName)
	--查找英雄的标志
	--wzry_outcry_heroTip.bmp
	
	if num == 0 then
		num = 1
	end
	local tempx = 0
	local tempy = 0
	local tempNum = 0
	--循环喊话任务
	
	local  tempNum = 0
	dm_OBJ:LuaSleep(3000)
	while true do
		inputLog(hwnd,"[喊话子程序]在循环找关闭标志")
		
		ret = dm_OBJ:FindPicEx(369,30,720,400,"wzry_loaded_closeWindow.bmp|wzry_loaded_closeWindow1.bmp|wzry_loaded_closeWindow2.bmp|wzry_loaded_closeWindow3.bmp|wzry_loaded_closeWindow4.bmp","050505",0.82,0)
		if ret ~= "" then
			break
		end
		dm_OBJ:LuaSleep(500)
	end
	
	
	while true do
		
		--wzry_loaded_closeWindow.bmp
		dm_OBJ:LuaSleep(1000)
		ret = dm_OBJ:FindPicEx(369,30,720,400,"wzry_loaded_closeWindow.bmp|wzry_loaded_closeWindow1.bmp|wzry_loaded_closeWindow2.bmp|wzry_loaded_closeWindow3.bmp|wzry_loaded_closeWindow4.bmp","050505",0.82,0)
		if ret ~= "" then
			inputLog(hwnd,"[喊话子程序]找到了关闭标志")
			tempNum = tempNum + 1
			stable = Split(ret,"|")
			if stable[2] ~= nil then
				stable = Split(stable[1],",")
			else
				stable = Split(ret,",")
			end
			
			
			
			
			
			if (tonumber(stable[2])>0) then
				--Found this pic ,click this pic
				dm_OBJ:MoveTo(tonumber(stable[2]+5),tonumber(stable[3]+5))
				dm_OBJ:LuaSleep(300)
				dm_OBJ:LeftClick()
				dm_OBJ:LuaSleep(500)
				
				
				inputLog(hwnd,"[喊话子程序]找到了关闭的标志了")
			end
			dm_OBJ:LuaSleep(1000)
		end
		
		
		local ret =dm_OBJ:FindPicEx(0,0,100,500,"wzry_outcry_heroTip.bmp","050505",0.8,0)
		
		if ret ~= "" then
			inputLog(hwnd,"[喊话子程序]找到了喊话标志了")
			break
		end
		
		
		--App无响应
		--237,177,495,332
		ret = dm_OBJ:FindPicEx(237,177,495,332,"wzry_loaded_unanswer.bmp","050505",0.8,0)
		
		if ret ~= "" then
			inputLog(hwnd,"[喊话子程序]App无响应动作")
			stable = Split(ret,",")
			if (tonumber(stable[2])>0) then
				dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
				dm_OBJ:LuaSleep(500)
				dm_OBJ:LeftClick()
				dm_OBJ:LuaSleep(1000)
				
			end
		end
		
		if tempNum >10 then
			closeApp(dm_OBJ,hwnd)
			
			dm_OBJ:LuaSleep(1000)
			local ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_desktop.bmp","050505",0.9,0)
			if ret ~= "" then
				stable = Split(ret,",")
				if (tonumber(stable[2])>0) then
					inputLog(hwnd,"[喊话子程序]找到了桌面图标")
					inputLog(hwnd,ret)
					--Found this pic ,click this pic
					dm_OBJ:MoveTo(tonumber(stable[2]),tonumber(stable[3]))
					dm_OBJ:LuaSleep(1000)
					dm_OBJ:LeftClick()
					--wait for QQload pic tip
					
					
					while true do
						inputLog(hwnd,"[喊话子程序]准备查找开始游戏标志")
						dm_OBJ:LuaSleep(500)
						ret = dm_OBJ:FindPicEx(256,324,486,402,"wzry_load_startGame.bmp","050505",0.9,0)
						if ret ~= "" then
							--362,358
							inputLog(hwnd,"[喊话子程序]开始游戏")
							dm_OBJ:MoveTo(362,358)
							dm_OBJ:LuaSleep(500)
							dm_OBJ:LeftClick()
							dm_OBJ:LuaSleep(500)
							
							while true do
								ret = dm_OBJ:FindPicEx(256,324,486,402,"wzry_load_startGame.bmp","050505",0.9,0)
								if ret ~= "" then
									inputLog(hwnd,"[喊话子程序]开始游戏")
									dm_OBJ:MoveTo(362,358)
									dm_OBJ:LuaSleep(500)
									dm_OBJ:LeftClick()
									dm_OBJ:LuaSleep(500)
								else
									tempNum = 0
									break
								end
								
							end
							
							break
						else
							inputLog(hwnd,"[喊话子程序]没有找到开始游戏")
							ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_load_myserver.bmp","050505",0.8,0)
							if ret ~= "" then
								inputLog(hwnd,"[喊话子程序]找到了我的服务")
								dm_OBJ:MoveTo(207,159)
								dm_OBJ:LuaSleep(500)
								dm_OBJ:LeftClick()
								dm_OBJ:LuaSleep(500)
							end
							
							ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_load_Accept.bmp","050505",0.8,0)
							if ret ~= "" then
								stable = Split(ret,",")
								inputLog(hwnd,"[喊话子程序]找到了接受按钮")
								if (tonumber(stable[2])>0) then
									dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
									dm_OBJ:LuaSleep(500)
									dm_OBJ:LeftClick()
									dm_OBJ:LuaSleep(500)
									
								end
								
							end
							inputLog(hwnd,"[喊话子程序]继续循环找开始游戏")
							dm_OBJ:LuaSleep(1000)
						end
						
						dm_OBJ:LuaSleep(1000)
					end
					
					
				end
			else
				
				inputLog(hwnd,"[喊话子程序]没有找到桌面图标")
				return "没有找到图标"
			end
			
			tempNum = 0
		end
		
		
		
	end
	
	
	inputLog(hwnd,"[喊话子程序]准备喊话")
	while true do
		inputLog(hwnd,"[喊话子程序]在循环喊话中")
		local ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_outcry_heroTip.bmp","050505",0.8,0)
		if ret ~= "" then
			inputLog(hwnd,"[喊话子程序]找到英雄的标志")
			stable = Split(ret,",")
			if (tonumber(stable[2])>0) then
				dm_OBJ:MoveTo(tonumber(stable[2])+30,tonumber(stable[3])-30)
				dm_OBJ:LuaSleep(1000)
				dm_OBJ:LeftClick()
				dm_OBJ:LuaSleep(3000)
				--查找麦克风开始
				ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_chat_Iknow.bmp","050505",0.9,0)
				if ret ~= "" then
					stable = Split(ret,",")
					if (tonumber(stable[2])>0) then
						dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
						dm_OBJ:LuaSleep(500)
						dm_OBJ:LeftClick()
						dm_OBJ:LuaSleep(1000)
					end
					
				end
				tempNum = 0
				while true do
					--查找麦克风的标志
					
					--c63839-050505
					--FindColorEx
					ret =dm_OBJ:FindColorEx(62,398,181,423,"ab3335-050505|bd3737-050505",0.9,0)
					
					if ret ~= "" then
						inputLog(hwnd,"[喊话子程序]喊话等待..")
						dm_OBJ:LuaSleep(1000)
					else
						--inputLog("开始喊话,内容为:" .. connet )
						ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_outcry_mic.bmp","050505",0.8,0)
						if ret ~= "" then
							stable = Split(ret,",")
							if (tonumber(stable[2])>0) then
								tempx = tonumber(stable[2])+70
								tempy = tonumber(stable[3])+10
								dm_OBJ:MoveTo(tempx,tempy)
								dm_OBJ:LuaSleep(1000)
								dm_OBJ:LeftClick()
								dm_OBJ:LuaSleep(1000)
								
								dm_OBJ:MoveTo(tonumber(stable[2])+70,tonumber(stable[3])+10)
								
								dm_OBJ:KeyPress(8)
								dm_OBJ:LuaSleep(1000)
								dm_OBJ:SendString(tonumber(hwnd),connet.."*"..tostring(tempNum))
								dm_OBJ:LuaSleep(1000)
								while true do
									--查找确定按钮
									ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_outcry_ascertain.bmp","050505",0.8,0)
									if ret ~= "" then
										
										stable = Split(ret,",")
										if (tonumber(stable[2])>0) then
											dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
											dm_OBJ:LuaSleep(1000)
											dm_OBJ:LeftClick()
											dm_OBJ:LuaSleep(1000)
											while true do
												inputLog(hwnd,"[喊话子程序]发送消息")
												ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_outcry_send.bmp","050505",0.8,0)
												if ret ~= "" then
													stable = Split(ret,",")
													if (tonumber(stable[2])>0) then
														dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
														dm_OBJ:LuaSleep(500)
														dm_OBJ:LeftClick()
														dm_OBJ:LuaSleep(2000)
														while true  do
															ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_outcry_send.bmp","050505",0.8,0)
															if ret ~= "" then
																break																
															end
															ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_outcry_sure.bmp","050505",0.75,0)
															if ret ~= "" then
																stable = Split(ret,",")
																if (tonumber(stable[2])>0) then
																	dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
																	dm_OBJ:LuaSleep(300)
																	dm_OBJ:LeftClick()
																	--上传报告
																	SubmitReport(dm_OBJ,userName,0)
																	return "the Account is forbid!"
																end
																
															end
															dm_OBJ:LuaSleep(1000)
														end
														
														
														break
													end
												end
												
											end
											
											break
										end
									end
									
									dm_OBJ:LuaSleep(1000)
								end
								
								
								tempNum = tempNum+1
							end
							
							
							
							dm_OBJ:LuaSleep(1000)
						end
						
					end
					
					
					if tempNum>= num then
						inputLog(hwnd,"[喊话子程序]达到次数,准备退出!次数:"..tostring(num))
						SubmitReport(dm_OBJ,userName,1)
						break
					end
					
				end
				
				
				--这里需要退出聊天窗口
				while true do
					ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_outcry_closeChat.bmp","050505",0.9,0)
					if ret ~= "" then
						stable = Split(ret,",")
						if (tonumber(stable[2])>0) then
							dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
							dm_OBJ:LuaSleep(1000)
							dm_OBJ:LeftClick()
							dm_OBJ:LuaSleep(1000)
							break
							
						end
						
					end
					
					
				end
				
				return nil
				
			end
			
			
			--return "break the loop"
			--结束查找麦克风
		else
			inputLog(hwnd,"[喊话子程序]关闭所有干扰")
			--关闭所有干扰
			
		end
	end
	
	
	
	
	
	
end

function yunDaMa(dm_OBJ,rcpath,hwnd)
	local str = ""
	local damaID = 0
	local d_path = rcpath .. "lua\\rc\\kingRobot\\"
	--打码范围
	--198,156,364,223
	while true do
		inputLog(hwnd,"准备打码")
		--wzry_yundama_enterCodeTip.bmp
		dm_OBJ:LuaSleep(1000)
		ret = dm_OBJ:FindPicEx(367,160,513,218,"wzry_yundama_enterCodeTip.bmp","050505",0.9,0)
		if ret ~= "" then
			inputLog(hwnd,"开始打码")
			--这里开始截图
			--传图片
			--取识别信息
			--下面是准备输入信息
			
			while true do
				dm_OBJ:Capture(198,156,364,223,tostring(hwnd)..".bmp")
				
				
				dm_OBJ:LuaSleep(500)
				dm_OBJ:YDM_SetAppInfo(3512,"a2060321e612b8694a53bc1d007ad3b6")
				dm_OBJ:YDM_Login("ycjl520", "weiyudns")
				dm_OBJ:LuaSleep(500)
				damaID = dm_OBJ:YDM_UploadByPath(d_path..tostring(hwnd)..".bmp", 1004)
				dm_OBJ:LuaSleep(3000)
				--return damaID
				--end
				
				while true do
					
					local tempstr = dm_OBJ:YDM_GetResult(tonumber(damaID))
					--return tempstr
					
					local tempTable = {}
					tempTable = Split(tempstr,"|")
					
					if tempTable[1] == "0" then
						str=tempTable[2]
						break
						
					elseif  tempTable[1] == "-1007"then
						str = "余额不足"
						break
					end
					
					dm_OBJ:LuaSleep(1000)
					
				end
				
				
				if str ~= "" then
					break
				end
				
				--end
				--return str
			end
			
			
			
			
			if str ~= "" then
				--这里是点击确认验证码
				
				if str == "余额不足" then
					--67,71
					dm_OBJ:MoveTo(67,71)
					dm_OBJ:LuaSleep(1000)
					dm_OBJ:LeftClick()
					
				else
					dm_OBJ:MoveTo(420,188)
					dm_OBJ:LuaSleep(1000)
					dm_OBJ:LeftClick()
					dm_OBJ:LuaSleep(1000)
					dm_OBJ:SendString(tonumber(hwnd),str)
					dm_OBJ:MoveTo(356,330)
					dm_OBJ:LuaSleep(1000)
					dm_OBJ:LeftClick()
					dm_OBJ:LuaSleep(3000)
				end
				
			end
			os.remove(rcpath.."lua\\rc\\kingRobot\\"..tostring(hwnd)..".bmp")
			
		else
			inputLog(hwnd,"没有打码")
			break
			
		end
		
		dm_OBJ:LuaSleep(1000)
		inputLog(hwnd,"循环打码")
	end
	return str
end


function QQLoadAccount(dm_OBJ,rcpath,hwnd,name,password)
	inputLog(hwnd,name)
	inputLog(hwnd,password)
	local str = ""
	local tempNum = 0
	dm_OBJ:LuaSleep(1000)
	--dm_OBJ:Capture(0, 0, 2000, 2000, "c:\测试.bmp")
	local ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_desktop.bmp","050505",0.9,0)
	if ret ~= "" then
		stable = Split(ret,",")
		if (tonumber(stable[2])>0) then
			inputLog(hwnd,"[登陆子程序]找到了桌面图标")
			inputLog(hwnd,ret)
			--Found this pic ,click this pic
			dm_OBJ:MoveTo(tonumber(stable[2]),tonumber(stable[3]))
			dm_OBJ:LuaSleep(1000)
			dm_OBJ:LeftClick()
			--wait for QQload pic tip
			while true do
				ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_load_QQTip.bmp","050505",0.8,0)
				if ret ~= "" then
					-- Found this pic tip, click this pic
					inputLog(hwnd,"[登陆子程序]找到了登陆按钮")
					stable = Split(ret,",")
					if (tonumber(stable[2])>0) then
						dm_OBJ:MoveTo(tonumber(stable[2]),tonumber(stable[3]))
						dm_OBJ:LuaSleep(1000)
						dm_OBJ:LeftClick()
						dm_OBJ:LuaSleep(1000)
						--wait QQload tip the pic
						while true do
							--wzry_QQload_Tip.bmp
							ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_QQload_Tip.bmp|wzry_QQload_Tip1.bmp","050505",0.8,0)
							
							if ret ~= "" then
								--314,105
								--dm_OBJ:MoveTo(314,105)
								--dm_OBJ:LuaSleep(500)
								--dm_OBJ:LeftClick()
								dm_OBJ:LuaSleep(1000)
								for i = 1, 5 do
									ret = dm_OBJ:FindPicEx(291,184,434,241,"wzry_account_zhanghaomimadenglu.bmp","050505",0.8,0)
									if ret ~= "" then
										inputLog(hwnd,"[登陆子程序]找到了切换账号:"..tostring(ret))
										stable = Split(ret,",")
										--120,40
										dm_OBJ:MoveTo(tonumber(stable[2])+3,tonumber(stable[3])+3)
										dm_OBJ:LuaSleep(1000)
										dm_OBJ:LeftClick()
										dm_OBJ:LuaSleep(1000)
										break
									else
										inputLog(hwnd,"[登陆子程序]没有找到切换账号")
									end
								end
								
								
								
								ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_QQload_Tip.bmp|wzry_QQload_Tip1.bmp","050505",0.9,0)
								if ret ~="" then
									--这里是判断是否到达输入账号的页面
									--wait the
									while true do
										
										--wzry_account_init.bmp
										ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_account_init.bmp","050505",0.9,0)
										if ret ~= "" then
											inputLog(hwnd,"[登陆子程序]找到输入账号的文本框")
											--输入账号密码
											inputAccount(dm_OBJ,rcpath,hwnd,name,password)
											
											inputLog(hwnd,"[登陆子程序]准备查找开始按钮")
											tempNum = 0
											while true do
												inputLog(hwnd,"[登陆子程序]准备查找开始游戏标志")
												dm_OBJ:LuaSleep(500)
												ret = dm_OBJ:FindPicEx(256,324,486,402,"wzry_load_startGame.bmp","050505",0.9,0)
												if ret ~= "" then
													--362,358
													inputLog(hwnd,"[登陆子程序]开始游戏")
													dm_OBJ:MoveTo(362,358)
													dm_OBJ:LuaSleep(500)
													dm_OBJ:LeftClick()
													dm_OBJ:LuaSleep(500)
													
													while true do
														ret = dm_OBJ:FindPicEx(256,324,486,402,"wzry_load_startGame.bmp","050505",0.9,0)
														if ret ~= "" then
															inputLog(hwnd,"[登陆子程序]开始游戏")
															dm_OBJ:MoveTo(362,358)
															dm_OBJ:LuaSleep(500)
															dm_OBJ:LeftClick()
															dm_OBJ:LuaSleep(500)
															
															break
														end
														dm_OBJ:LuaSleep(500)
													end
													
													break
												else
													inputLog(hwnd,"[登陆子程序]没有找到开始游戏")
													ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_load_myserver.bmp","050505",0.8,0)
													if ret ~= "" then
														inputLog(hwnd,"[登陆子程序]找到了我的服务")
														dm_OBJ:MoveTo(207,159)
														dm_OBJ:LuaSleep(500)
														dm_OBJ:LeftClick()
														dm_OBJ:LuaSleep(500)
													end
													
													ret =dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_load_Accept.bmp","050505",0.8,0)
													if ret ~= "" then
														stable = Split(ret,",")
														inputLog(hwnd,"[登陆子程序]找到了接受按钮")
														if (tonumber(stable[2])>0) then
															dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
															dm_OBJ:LuaSleep(500)
															dm_OBJ:LeftClick()
															dm_OBJ:LuaSleep(500)
															
														end
														
													end
													if tempNum > 10 then
														inputLog(hwnd,"[登陆子程序]超过查找次数,准备退出")
														break
													end
													tempNum = tempNum + 1
													inputLog(hwnd,"[登陆子程序]继续循环找开始游戏")
													dm_OBJ:LuaSleep(1000)
												end
												--清除数据
												inputLog(hwnd,"清除原有数据")
												clearAccount(dm_OBJ,hwnd)
												
												--登陆账号
												inputLog(hwnd,"重新输入账号密码")
												inputAccount(dm_OBJ,rcpath,hwnd,name,password)
												
												dm_OBJ:LuaSleep(1000)
												inputLog(hwnd,"输入完毕")
												
											end
											break
										else
											dm_OBJ:LuaSleep(1000)
											ret =dm_OBJ:FindPicEx(275,242,453,317,"wzry_load_ErrAccount.bmp","050505",0.8,0)
											
											if ret ~= "" then
												-- Found this pic tip, click this pic
												inputLog(hwnd,"[登陆子程序]账号密码错误")
												stable = Split(ret,",")
												if (tonumber(stable[2])>0) then
													dm_OBJ:MoveTo(tonumber(stable[2]),tonumber(stable[3]))
													dm_OBJ:LuaSleep(1000)
													dm_OBJ:LeftClick()
													dm_OBJ:LuaSleep(1000)
													
												end
											end
											dm_OBJ:LuaSleep(1000)
											--清除数据
											inputLog(hwnd,"初始清除数据!!!")
											clearAccount(dm_OBJ,hwnd)
											
										end
										
										dm_OBJ:LuaSleep(1000)
										
										
										
									end
									
									break
								end
								
							else
								inputLog(hwnd,"[登陆子程序]没有找到QQ登陆提示")
								ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_load_Accept.bmp","050505",0.8,0)
								if ret ~= "" then
									stable = Split(ret,",")
									--120,40
									if tonumber(stable[2])>0 then
										dm_OBJ:MoveTo(tonumber(stable[2])+10,tonumber(stable[3])+10)
										dm_OBJ:LuaSleep(1000)
										dm_OBJ:LeftClick()
										dm_OBJ:LuaSleep(1000)
									end
								end
								
							end
							
							dm_OBJ:LuaSleep(1000)
						end
						
						--aa
					end
					break
				end
				
				ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_load_logout.bmp","050505",0.8,0)
				if ret ~= "" then
					stable = Split(ret,",")
					--120,40
					dm_OBJ:MoveTo(tonumber(stable[2])+5,tonumber(stable[3])+5)
					dm_OBJ:LuaSleep(1000)
					dm_OBJ:LeftClick()
					dm_OBJ:LuaSleep(1000)
				end
				
				dm_OBJ:LuaSleep(1000)
			end
			
		end
	else
		
		inputLog(hwnd,"[登陆子程序]没有找到桌面图标")
		return "没有找到图标"
	end
	
	
end

function inputAccount(dm_OBJ,rcpath,hwnd,name,password)
	inputLog(hwnd,"[输入账号子程序]输入账号密码1:" .. name .. ","..password)
	local num = 0
	while true do
		dm_OBJ:LuaSleep(1000)
		ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_account_init.bmp","050505",0.8,0)
		if ret ~= "" then
			inputLog(hwnd,"[输入账号子程序]输入账号密码2:" .. name .. ","..password)
			stable = Split(ret,",")
			if tonumber(stable[2])>0 then
				--120,40
				
				dm_OBJ:MoveTo(113,143)
				dm_OBJ:LuaSleep(500)
				dm_OBJ:LeftClick()
				dm_OBJ:LuaSleep(500)
				
				dm_OBJ:SendString(tonumber(hwnd),name)
				
				dm_OBJ:LuaSleep(1000)
				
				dm_OBJ:KeyPress(9)
				
				dm_OBJ:LuaSleep(1000)
				inputLog(hwnd,"[输入账号子程序]输入账号" )
				dm_OBJ:SendString(tonumber(hwnd),password)
				
				
				dm_OBJ:LuaSleep(1000)
				inputLog(hwnd,"[输入账号子程序]输入密码" )
				dm_OBJ:MoveTo(359,222)
				dm_OBJ:LeftClick()
				dm_OBJ:LuaSleep(5000)
				
				str = yunDaMa(dm_OBJ,rcpath,hwnd)
				if str == "余额不足" then
					
					return str
				end
				inputLog(hwnd,"[输入账号子程序]输入账号密码完毕,退出函数" )
				break
			end
		else
			inputLog(hwnd,"[输入账号子程序]没有找到输入账号密码")
			num = num+1
		end
		dm_OBJ:LuaSleep(1000)
		if num >= 3 then
			break
		end
	end
	
end

function clearAccount(dm_OBJ,hwnd)
	
	dm_OBJ:LuaSleep(3000)
	ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_QQload_clear.bmp|wzry_QQload_clear1.bmp","050505",0.9,0)
	
	if ret ~= "" then
		inputLog(hwnd,"[清理账号子程序]找到清除信息标志")
		while true do
			inputLog(hwnd,"[清理账号子程序]循环清除数据")
			ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_QQload_clear.bmp|wzry_QQload_clear1.bmp","050505",0.9,0)
			inputLog(hwnd,ret)
			if ret ~= "" then
				stable = Split(ret,"|")
				if stable[2] ~= nil then
					stable = Split(stable[1],",")
				else
					stable = Split(ret,",")
				end
				
				
				--120,40
				if tonumber(stable[2])>0 then
					dm_OBJ:MoveTo(tonumber(stable[2]+5),tonumber(stable[3]+5))
					dm_OBJ:LuaSleep(1000)
					dm_OBJ:LeftClick()
					dm_OBJ:LuaSleep(1000)
				end
				
			end
			
			
			ret = dm_OBJ:FindPicEx(0,0,2000,2000,"wzry_QQload_clear.bmp|wzry_QQload_clear1.bmp","050505",0.9,0)
			if ret == "" then
				break
			end
			dm_OBJ:LuaSleep(1000)
		end
		inputLog(hwnd,"[清理账号子程序]清除数据完毕!")
	end
	
end


function closeApp(dm_OBJ,hwnd)
	local num = 0
	
	while true do
		inputLog(hwnd,"[关闭应用子程序]准备关闭App")
		dm_OBJ:LuaSleep(500)
		dm_OBJ:MoveTo(741,375)
		dm_OBJ:LuaSleep(300)
		dm_OBJ:LeftClick()
		dm_OBJ:LuaSleep(3000)
		ret =dm_OBJ:FindPicEx(13,120,723,256,"wzry_closeApp.bmp|wzry_closeApp1.bmp","050505",0.8,0)
		if ret ~= "" then
			inputLog(hwnd,"[关闭应用子程序]找到了关闭图标")
			
			stable = Split(ret,"|")
			if stable[2] ~= nil then
				stable = Split(stable[1],",")
			else
				stable = Split(ret,",")
			end
			if tonumber(stable[2])>0 then
				
				dm_OBJ:MoveTo(tonumber(stable[2]+20),tonumber(stable[3]+20))
				dm_OBJ:LuaSleep(500)
				dm_OBJ:LeftDown()
				--dm_OBJ:LuaSleep(30)
				dm_OBJ:MoveR(0,-100)
				--dm_OBJ:LuaSleep(500)
				dm_OBJ:LeftUp()
				dm_OBJ:LuaSleep(500)
				--关闭现有窗口
				dm_OBJ:MoveTo(17,47)
				dm_OBJ:LuaSleep(500)
				dm_OBJ:LeftClick()
				dm_OBJ:LuaSleep(500)
				inputLog(hwnd,"[关闭应用子程序]已执行关闭APP动作")
				return "close windows"
			end
			
			
			
		else
			--636,251
			dm_OBJ:MoveTo(17,47)
			dm_OBJ:LuaSleep(500)
			dm_OBJ:LeftClick()
			dm_OBJ:LuaSleep(500)
			
			num = num + 1
			
		end
		
		if num >= 5 then
			inputLog(hwnd,"[关闭应用子程序]没有找到APP程序了")
			return "close windows"
		end
		
		dm_OBJ:LuaSleep(500)
	end
	
end

function inputLog(hwnd,adata)
	--local thisTime = os.time()
	
	
	local data = ""
	local file = io.open(g_rcpath .."log\\".. tostring(hwnd) .. "log.txt","r")
	if file ~= nil then
		data = file:read("*a")
		file:close()
		
		
	end
	
	file = io.open(g_rcpath.."log\\".. tostring(hwnd) .."log.txt","w")
	file:write(data.. "\n" ..tostring(hwnd) .. ": "..adata)
	file:close()
	
end

function Main(dm_OBJ,hwnd,rcpath)
	--return dm_OBJ:YDM_Login("ycjl520", "weiyudns");
	-- ydm.YDM_SetAppInfo("2842", "378d9f20d3a9a534c40c68196cca1632");
	-- return ydm.YDM_Login("ycjl520", "weiyudns");
	g_rcpath = rcpath
	
	
	
	local x = 0
	local y = 0
	local ret = ""
	local stable = {}
	local name = ""
	local password = ""
	
	local myAccountTable = {}
	--ret = dm_OBJ:Get("http://192.168.1.27/ajax/data/data_qq.ashx?action=getdata_qq&&count=1")
	--name, password = getUserPasw(dm_OBJ)
	--ret = "name:"..name .. " password:" .. password
	
	--inputLog(hwnd,"开始1")
	--myAccountTable = readAccount(dm_OBJ,rcpath,hwnd)
	
	--if #myAccountTable == 0 then
	--	return "no account"
	--end
	dm_OBJ:SetPath(rcpath .. 'lua\\rc\\kingRobot\\')
	--inputLog(hwnd,"开始2")
	
	--绑定窗口句柄
	
	
	local bindret = dm_OBJ:BindWindow(tonumber(hwnd), "dx3", "windows","windows", 0)
	inputLog(hwnd,"[主程序]窗口绑定状态:" .. tostring(bindret))
	if tonumber(bindret) == 0 then
		return "UnBind"
	end
	
	dm_OBJ:LuaSleep(2000)
	
	
	inputLog(hwnd,"[主程序]开始")
	--closeApp(dm_OBJ,hwnd)
	

	fDate = readtxt(dm_OBJ,rcpath.."config/",hwnd,"Label_0")
	ftime = readtxt(dm_OBJ,rcpath.."config/",hwnd,"Label_1")
	inputLog(hwnd,"[主程序]初始外部数据")
	fruntime = readtxt(dm_OBJ,rcpath.."config/",hwnd,"Label_2")
	if ftime == "" then
		ftime ="1"
	end
	if fruntime =="" then
		fruntime = "1"
	end
	inputLog(hwnd,"查看喊话次数" .. tostring(ftime))
	
	
	for i = 1, tonumber(fruntime) do
		inputLog(hwnd,"[主程序]准备获取账号密码")
		name, password = getUserPasw(dm_OBJ,hwnd)
		if name == "" then
			return "Not Account"
		end
		inputLog(hwnd,"[主程序]获取账号密码成功")
		inputLog(hwnd,password)
		--ret = "what"
		ret =QQLoadAccount(dm_OBJ,rcpath,hwnd,name,password)
		inputLog(hwnd,"[主程序]登陆账号完毕")
		if ret == "余额不足" then
			return "balance lacking"
		elseif ret == "没有找到图标" then
			return "UnFindPic"
		end
		dm_OBJ:LuaSleep(3000)
		
		inputLog(hwnd,"[主程序]准备喊话1")
		--inputLog("喊话数据,内容:" .. fDate .. " 次数:" .. ftime)
		ret = Outcry(dm_OBJ,hwnd,fDate,tonumber(ftime),name)
		inputLog(hwnd,"[主程序]喊话完毕")
		--ret = yunDaMa(dm_OBJ,rcpath,hwnd)
		
		
		closeApp(dm_OBJ,hwnd)
		
	end
	
	dm_OBJ:UnBindWindow()
	
	
	return(ret)
	
	
	
	
end
--Main(dm_OBJ,hwnd,rcpath)






function getUserPasw(dm_OBJ,hwnd) --获取网络上的账号密码,返回的是:账号,密码
	local content = ""
	local username = ""
	local password = ""
	local tempData =""
	--content = [[{"id":4,"qquser":"2434731610","qqpwd":"ww222222","ipaddress":"","state":1,"statetime":"","status":1,"getcount":0,"errlog":"","stateStr":"正常"}]]
	
	content = dm_OBJ:Get("http://wz.26a.com/ajax/data/data_qq.ashx?action=getdata_qq&&count=1")
	if content == "[]" then
		return "",""
	end
	
	if string.find(content,"无法连接") ~= nil then
		return "",""
	end
	
	
	inputLog(hwnd,"[getUserPasw]获取到的数据:" .. tostring(content))
	tempData = string.match(content,'"qquser":"%d+"')
	if tempData == nil then
		return "",""
	end
	username = string.match(tempData,'%d+')
	
	tempData = string.match(content,'"qqpwd":"%w*%d*%w*%d*"')
	if tempData == nil then
		return "",""
	end
	--tempData = Split(tempData,",")[1]
	tempData = Split(tempData,":")[2]
	password = string.sub(tempData,2,-2)

	return username,password
end

function SubmitReport(dm_OBJ,userName,state) -- 提交账号状态,userName是QQ账号;state是状态是string数据类型,0为异常,1为正常
	
	content = dm_OBJ:Get("http://wz.26a.com/ajax/data/data_qq.ashx?action=editdata_qq&&qquser=" .. userName .. "&&state=" .. state .. "&&ipaddress=&&errlog=")
	
end

--getUserPasw("aa")